﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditUserFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditUserFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_User> GetUser(int id)
        {
            AjaxResult<TB_User> result = new AjaxResult<TB_User>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetUser(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveUser(string id, string code, string userName, string post, string mobile,string  tel, string pwd, string role, string sex, string leader)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {

                TB_User user = new TB_User();
                if (update)
                    user = this.BaseDataProxy.GetUser(Convert.ToInt32(id));

                WebCommon.EvalControlValueToData(user, "post", post);
                WebCommon.EvalControlValueToData(user, "role", role);
                WebCommon.EvalControlValueToData(user, "sex", sex);
                WebCommon.EvalControlValueToData(user, "tel", tel);
                WebCommon.EvalControlValueToData(user, "mobile", mobile);
                WebCommon.EvalControlValueToData(user, "userName", userName);
                WebCommon.EvalControlValueToData(user, "role", role);

                if (update)
                {
                    this.BaseDataProxy.UpdateUser(user);
                    result.Data = user.id;
                }
                else
                {
                    WebCommon.EvalControlValueToData(user, "code", code);
                    user.pwd = string.IsNullOrEmpty(pwd) ? null : EncryptCommon.MD5Encrypt(pwd);
                    if (this.BaseDataProxy.ExistUserCode(user.code))
                    {
                        result.Msg ="用户账号已存在，请另起账号！";
                        return result;
                    }
                    user.deleteFlag = false;
                    result.Data = this.BaseDataProxy.AddUser(user);
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteUser(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                //if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                //{
                //    result.Msg = "没有操作权限！";
                //    return result;
                //}

                result.Data = this.BaseDataProxy.DeleteUser(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
